from django.shortcuts import render,redirect
from django.views import View

# 获取广告内容类别
from contents.models import ContentCategory
from contents.utils import get_categories
from collections import OrderedDict
from goods.models import GoodsChannel

# 引入商品表中的sku数据
from goods.models import SKU

class ImageView(View):
    def get(self,request):

        # 从数据库中获取sku数据
        skus = SKU.objects.all()
        # 创建一个列表，用来保存取出的数据
        skus_list = []
        # 遍历出sku中的数据
        for sku in skus:
            skus_list.append({
                # 取出图片
                'img_url':sku.default_image.url
            })
        return render(request,'img.html',{'skus':skus_list})
#-------------------首页的分类导航展示------------------------
class IndexView(View):
    def get(self,request):
        """
{
    "1":{
        "channels":[
            {"id":1, "name":"手机", "url":"http://shouji.jd.com/"},
            {"id":2, "name":"相机", "url":"http://www.itcast.cn/"}
        ],
        "sub_cats":[
            {
                "id":38,
                "name":"手机通讯",
                "sub_cats":[
                    {"id":115, "name":"手机"},
                    {"id":116, "name":"游戏手机"}
                ]
            },
            {
                "id":39,
                "name":"手机配件",
                "sub_cats":[
                    {"id":119, "name":"手机壳"},
                    {"id":120, "name":"贴膜"}
                ]
            }
        ]
    },
    "2":{
        "channels":[],
        "sub_cats":[]
    }
}

        """
        # 1.渲染分类导航数据
        categories = get_categories()

        # # 2.渲染广告数据
        contents = {}
        # 查询所有广告分类
        contentcategorys = ContentCategory.objects.all()
        for contentcategory in contentcategorys:
            contents[contentcategory.key] = contentcategory.content_set.filter(status=True).order_by('sequence')
        data = {
            'categories':categories,
            'contents':contents
        }

        return render(request,'index.html',data)















































